package com.xmall.utils;

import com.xmall.define.constants.CommonConstants;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

public class AnnotationUtils {

    /**
     * 扫描自定义注解类
     */
    public static Set<Class<?>> getAnnotationClass(Class<? extends Annotation> annotationClass) {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(annotationClass));
        Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(CommonConstants.BASE_PACKAGE);
        Set<Class<?>> classes = new HashSet<>();
        for (BeanDefinition candidateComponent : candidateComponents) {
            Class<?> clazz = ClassUtils.resolveClassName(candidateComponent.getBeanClassName(), AnnotationUtils.class.getClassLoader());
            classes.add(clazz);
        }
        return classes;
    }
}
